// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: implementation of CImplCommonChild.
//   $Id$


#include "stdafx.h"
#include "commonchild.h"
#include "formcontrolinputUI.h"

CImplCommonChild::~CImplCommonChild()
{	
}	

HRESULT CImplCommonChild::refresh()
{	
	//default behaviour is to do nothing - eg.
	//	help and alert
	FireStateEvents();
	return S_OK;
}

HRESULT CImplAlert::refresh()
{
	HRESULT hr = S_OK;
	if(m_bShouldRefresh && m_pParent) {
		IItemWithAlert *pContainer = dynamic_cast<IItemWithAlert*>(m_pParent);
		if(pContainer) {
			// m_pParent does implement ItemWithAlert, so get the value and tell parent.
			CComBSTR s;
			GetMultipleTextSource(&s);
			HRESULT hr = pContainer->onAlertValueChange(s);
			// ::SysFreeString(s); Is a CComBSTR so this is not required.
			if(hr == S_OK)
				m_bShouldRefresh = !!m_sRef.Length() || !!m_sBind;
		} else
			hr = S_FALSE;
	}
	FireStateEvents();
	return hr;
}

HRESULT CImplAlert::doInitialize()
{
	return CImplCommonChild::doInitialize();
}

HRESULT CImplCaption::refresh()
{
	
	if(m_bShouldRefresh && m_pParent)
	{
		//m_pParent is stored as CImpl, so a dynamic cast is needed.
		//	to reach the IItemWithCaption interface.
		IItemWithCaption * pContainer = dynamic_cast<IItemWithCaption*>(m_pParent);

		if(pContainer)
		{
			//m_pParent does implement the ItemWithCaption interface, get
			//	the value and tell the parent about it.
			CComBSTR s;
			GetMultipleTextSource(&s);
			HRESULT hr = pContainer->onCaptionValueChange(s);
			// ::SysFreeString(s); is a CComBSTR so not required.
			if(hr == S_OK)
			{
				//shouldRefresh is whether to refresh again, if we are from inline or
				//	external data (>xxx< or xlink:href="xxx") don't bother after the first time.
				m_bShouldRefresh = !isEmpty(m_sRef) || !isEmpty(m_sBind);
			}
			FireStateEvents();
			return hr;
		}
		else
		{
			FireStateEvents();
			return S_FALSE;
		}
	}
	else
	{
		FireStateEvents();
		return S_OK;
	}
}

CImplAlert::~CImplAlert()
{
	if(m_pParent)
	{
		IItemWithAlert * pParentWithAlert = dynamic_cast<IItemWithAlert*>(m_pParent); 
		if(pParentWithAlert)
		{
			pParentWithAlert->detachAlert();
		}
	}
}

CImplCaption::~CImplCaption() 
{
	if(m_pParent)
	{
		// Spotted when hunting this specified Bug 241.
		// Should always check a dynamic cast.
		IItemWithCaption * pItem = dynamic_cast<IItemWithCaption *>(m_pParent);
		if(pItem)
			pItem->detachCaption();
	}
};

